package com.iplatform.commons.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.iplatform.commons.Index;
import org.springframework.util.StringUtils;

/**
 *
 * Title: 字符串处理类 Description:
 *
 * @author zhouyuhua
 * @date 2019年4月15日
 */
public class MyStringUtils {
	public static Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
	public static Pattern alphabet = Pattern.compile(".*[a-zA-Z]+.*");

	/**
	 *
	 * Title: replaceSql Description: 把sql中${xxx}部分替换成formMap中key为xxx的值
	 *
	 * @param sql
	 * @param formMap
	 * @return
	 */
	public static String replaceSql(String sql, Map<String, String> formMap) {
		int a = sql.indexOf("${");
		int b = sql.indexOf("}", a);
		List<String> list = new ArrayList<>();
		List<String> list2 = new ArrayList<>();
		while (a != -1) {
			list.add(sql.substring(a, b + 1));
			a = sql.indexOf("${", a + 1);
			b = sql.indexOf("}", a);
		}
		for (int i = 0; i < list.size(); i++) {
			list2.add(list.get(i).substring(2, list.get(i).length() - 1));
		}
		for (int i = 0; i < list.size(); i++) {

			String s = formMap.get(list2.get(i)).toString();
			Matcher m = p.matcher(s);
			Matcher m1 = alphabet.matcher(s);
			if (m.find()) {
				if(!s.contains("'")) {
					s = "'" + s + "'";
				}
			} else {
				if (m1.find()) {
					if(!s.contains("'")) {
						s = "'" + s + "'";
					}
				}
			}
			sql = sql.replace(list.get(i), s);
		}

		return sql;
	}

	/**
	 *
	 * Title: getReplace Description: 获得替换占位后的sql
	 *
	 * @param map
	 * @param sql
	 * @param start
	 * @param end
	 * @return
	 */
	public static String getReplace(Map<String, String> map, String sql, String start, String end) {
		List<Index> list = getPlaceholderIndex(sql, start, end);
		String planYear = map.get("planYear") == null||map.get("planYear") == "" ? "" : map.get("planYear").toString();
		String year = map.get("year") == null||map.get("year") == "" ? "" : map.get("year").toString();
		String capitalTypeName = map.get("capitalTypeName") == null||map.get("capitalTypeName") == "" ? ""
				: "'" + map.get("capitalTypeName").toString() + "'";
		String specTopName = map.get("specTopName") == null||map.get("specTopName") == "" ? "" : map.get("specTopName").toString().contains("'")?map.get("specTopName").toString():"'"+map.get("specTopName").toString()+"'";
		String orgName = map.get("orgName") == null||map.get("orgName") == "" ? "" : "'" + map.get("orgName").toString() + "'";
		String orgId = map.get("orgId") == null||map.get("orgId") == "" ? "" : "" + map.get("orgId").toString() + "";
		String managerOrgId = map.get("managerOrgId") == null||map.get("managerOrgId") == "" ? "" : "" + map.get("managerOrgId").toString() + "";
		String orgUserId = map.get("orgUserId") == null||map.get("orgUserId") == "" ? "" : "" + map.get("orgUserId").toString() + "";
		String manageUserId = map.get("manageUserId") == null||map.get("manageUserId") == "" ? "" : "" + map.get("manageUserId").toString() + "";
		String followOrgId = map.get("followOrgId") == null||map.get("followOrgId") == "" ? "" : "" + map.get("followOrgId").toString() + "";
		String conSpec = map.get("conSpec") == null||map.get("conSpec") == "" ? "" : "'" + map.get("conSpec").toString() + "'";
		String office = map.get("office") == null||map.get("office") == "" ? "" : "'" + map.get("office").toString() + "'";
		String city = map.get("city") == null||map.get("city") == "" ? "" : "'" + map.get("city").toString() + "'";
		String address = map.get("address") == null||map.get("address") == "" ? "" : "'" + map.get("address").toString() + "'";
		String specId = "";
		String capitalName = map.get("capitalName") == null||map.get("capitalName") == "" ? "" : map.get("capitalName").toString().contains("'")?map.get("capitalName").toString():"'"+map.get("capitalName").toString()+"'";
		String isProvince = map.get("isProvince") == null||map.get("isProvince") == "" ? "" : map.get("isProvince").toString().contains("'")?map.get("isProvince").toString():"'"+map.get("isProvince").toString()+"'";
		if(!StringUtils.isEmpty(map.get("specId"))) {
			if(map.get("specId").contains("'")) {
				specId = map.get("specId");
			} else {
				specId = "'" + map.get("specId") + "'";
			}
		}
//		String specId = map.get("specId") == null||map.get("specId") == "" ? "" : "'" + map.get("specId").toString() + "'";
		String specId2 = map.get("specId2") == null||map.get("specId2") == "" ? "" : "" + map.get("specId2").toString() + "";
		String cityId = map.get("cityId") == null||map.get("cityId") == "" ? "" : "" + map.get("cityId").toString() + "";
		String officeId = map.get("officeId") == null||map.get("officeId") == "" ? "":"'" + map.get("officeId").toString() + "'";
		String managerOrgName = map.get("managerOrgName") == null||map.get("managerOrgName") == "" ? "":"'" + map.get("managerOrgName").toString() + "'";
		String userId = map.get("userId") == null||map.get("userId") == "" ? "" : "'" + map.get("userId").toString() + "'";
		List<String> sqlList = new ArrayList<>();
		List<String> sqlReplace = new ArrayList<>();
		int flagUser = 0;
		if(!StringUtils.isEmpty(orgUserId) && !StringUtils.isEmpty(manageUserId)) {
			flagUser = 1;
		}
		if(!StringUtils.isEmpty(orgUserId) && StringUtils.isEmpty(manageUserId)) {
			flagUser = 2;
		}
		if(StringUtils.isEmpty(orgUserId) && !StringUtils.isEmpty(manageUserId)) {
			flagUser = 3;
		}
		int flagOrg = 0;
		if(StringUtils.isEmpty(orgId) && StringUtils.isEmpty(managerOrgId)) {
			flagOrg = 1;
		}
		for (int i = 0; i < list.size(); i++) {
			String sqlbak = sql.substring(list.get(i).getStart(), list.get(i).getEnd() + "]".length());
			sqlList.add(sqlbak);
			if (sqlbak.contains("#{planYear}") && planYear != null && planYear != "") {
				sqlbak = sqlbak.replace("#{planYear}", planYear).replace(start, end).replace("]", "");
			}else if(sqlbak.contains("#{year}") && year != null && year != "") {
				sqlbak = sqlbak.replace("#{year}", year).replace(start, end).replace("]", "");
			} else if (sqlbak.contains("#{capitalTypeName}") && capitalTypeName != null && capitalTypeName != "") {
				sqlbak = sqlbak.replace("#{capitalTypeName}", capitalTypeName).replace(start, end).replace("]", "");
			} else if (sqlbak.contains("#{specTopName}") && specTopName != null && specTopName != "") {
				sqlbak = sqlbak.replace("#{specTopName}", specTopName).replace(start, end).replace("]", "");
			} else if (sqlbak.contains("#{orgName}") && orgName != null && orgName != "") {
				sqlbak = sqlbak.replace("#{orgName}", orgName).replace(start, end).replace("]", "");
			} else if (sqlbak.contains("#{orgId}") && orgId != null && orgId != "") {
				sqlbak = sqlbak.replace("#{orgId}", orgId).replace(start, end).replace("]", "");
			} else if (sqlbak.contains("#{managerOrgId}") && managerOrgId != null && managerOrgId != "") {
				sqlbak = sqlbak.replace("#{managerOrgId}", managerOrgId).replace(start, end).replace("]", "");
			}else if (sqlbak.contains("#{followOrgId}") && followOrgId != null && followOrgId != "" && flagOrg != 1) {
				 String field = sqlbak.replace("#{followOrgId}", "").replace(start, end).replace("]", "");
				 StringBuffer sb = new StringBuffer();
				 for(String follow : followOrgId.split(",")) {
					 if(!StringUtils.isEmpty(follow)) {
						 sb.append(" or ").append(field).append(" like '%").append(follow).append("%' ");
					 }
				 }
				sqlbak = sb.toString();
			}else if (sqlbak.contains("#{followOrgId}") && followOrgId != null && followOrgId != "" && flagOrg == 1) {
				 String field = sqlbak.replace("#{followOrgId}", "").replace(start, end).replace("]", "");
				 StringBuffer sb = new StringBuffer();
				 sb.append(" and (");
				 int j = 0;
				 for(String follow : followOrgId.split(",")) {
					 if(!StringUtils.isEmpty(follow)) {
						 if(j != 0) {
							 sb.append(" or ");
						 }
						 sb.append(field).append(" like '%").append(follow).append("%' ");
						 j++;
					 }
				 }
				 sb.append(" ) ");
				sqlbak =  sb.toString();
			} else if (sqlbak.contains("#{orgUserId}") && flagUser == 1) {
				String field = sqlbak.replace("#{orgUserId}", "").replace(start, end).replace("]", "");
				StringBuffer sb = new StringBuffer();
				sb.append(" and ( ").append(field).append(" = ").append(orgUserId).append(" ");
				sqlbak = sb.toString();
			}else if (sqlbak.contains("#{manageUserId}") && flagUser == 1) {
				String field = sqlbak.replace("#{manageUserId}", "").replace(start, end).replace("]", "");
				StringBuffer sb = new StringBuffer();
				sb.append(" or ").append(field).append(" = ").append(orgUserId).append(" )");
				sqlbak = sb.toString();
			}else if (sqlbak.contains("#{orgUserId}") && flagUser == 2) {
				String field = sqlbak.replace("#{orgUserId}", "").replace(start, end).replace("]", "");
				StringBuffer sb = new StringBuffer();
				sb.append(" and ").append(field).append(" = ").append(orgUserId);
				sqlbak = sb.toString();
			}else if (sqlbak.contains("#{manageUserId}") && flagUser == 3) {
				String field = sqlbak.replace("#{manageUserId}", "").replace(start, end).replace("]", "");
				StringBuffer sb = new StringBuffer();
				sb.append(" and ").append(field).append(" = ").append(manageUserId);
				sqlbak = sb.toString();
			}else if (sqlbak.contains("#{conSpec}") && conSpec != null && conSpec != "") {
				sqlbak = sqlbak.replace("#{conSpec}", conSpec).replace(start, end).replace("]", "");
			} else if (sqlbak.contains("#{office}") && office != null && office != "") {
				sqlbak = sqlbak.replace("#{office}", office).replace(start, end).replace("]", "");
			} else if (sqlbak.contains("#{city}") && city != null && city != "") {
				sqlbak = sqlbak.replace("#{city}", city).replace(start, end).replace("]", "");
			} else if (sqlbak.contains("#{address}") && address != null && address != "") {
				sqlbak = sqlbak.replace("#{address}", address).replace(start, end).replace("]", "");
			} else if (sqlbak.contains("#{specId}") && specId !=null && specId !="") {
				sqlbak = sqlbak.replace("#{specId}", specId).replace(start, end).replace("]", "");
			}else if (sqlbak.contains("#{specId2}") && specId2 !=null && specId2 !="") {
				sqlbak = sqlbak.replace("#{specId2}", specId2).replace(start, end).replace("]", "");
			}else if (sqlbak.contains("#{cityId}") && cityId !=null && cityId !="") {
					sqlbak = sqlbak.replace("#{cityId}", cityId).replace(start, end).replace("]", "");
			} else if (sqlbak.contains("#{officeId}") && officeId != null && officeId !=""){
				sqlbak = sqlbak.replace("#{officeId}", officeId).replace(start, end).replace("]", "");
			} else if (sqlbak.contains("#{managerOrgName}") && managerOrgName != null && managerOrgName !=""){
				sqlbak = sqlbak.replace("#{managerOrgName}", managerOrgName).replace(start, end).replace("]", "");
			} else if (sqlbak.contains("#{userId}") && userId != null && userId !=""){
				sqlbak = sqlbak.replace("#{userId}", userId).replace(start, end).replace("]", "");
			}else if (sqlbak.contains("#{capitalName}") && capitalName != null && capitalName !=""){
				sqlbak = sqlbak.replace("#{capitalName}", capitalName).replace(start, end).replace("]", "");
			}else if (sqlbak.contains("#{isProvince}") && isProvince != null && isProvince !=""){
				sqlbak = sqlbak.replace("#{isProvince}", isProvince).replace(start, end).replace("]", "");
			}else {
				sqlbak = sqlbak.replace(sqlbak, "");
			}
			sqlReplace.add(sqlbak);
		}
		for (int i = 0; i < sqlList.size(); i++) {
			sql = sql.replace(sqlList.get(i), sqlReplace.get(i));
		}
		return sql;
	}

	/**
	 *
	 * Title: getPlaceholderIndex Description: 获取&[和]的下标值
	 *
	 * @param s
	 * @param start
	 * @param end
	 * @return
	 */
	public static List<Index> getPlaceholderIndex(String s, String start, String end) {
		String sbak = s;
		String sbak2 = s;
		List<Integer> listStart = new ArrayList<>();
		List<Integer> listEnd = new ArrayList<>();
		int flag = 0;
		while (sbak.indexOf(start) != -1) {
			String aa = sbak.substring(0, sbak.indexOf(start) + start.length());
			flag = flag + aa.length();
			listStart.add(flag - start.length());
			sbak = sbak.substring(sbak.indexOf(start) + start.length());
		}
		flag = 0;
		while (sbak2.indexOf(end) != -1) {
			String aa = sbak2.substring(0, sbak2.indexOf(end) + end.length());
			flag = flag + aa.length();
			listEnd.add(flag - end.length());
			sbak2 = sbak2.substring(sbak2.indexOf(end) + end.length());
		}
		List<Index> list = new ArrayList<>();
		if (listStart.size() == listEnd.size()) {
			for (int i = 0; i < listStart.size(); i++) {
				Index index = new Index();
				index.setStart(listStart.get(i));
				index.setEnd(listEnd.get(i));
				list.add(index);
			}
		} else {
			list = null;
		}
		return list;
	}
}
